home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / cmplibsr.zoo / $index1.P < prev    next >
Text File  |  1988-09-15  |  6KB  |  171 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona,1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /*   "index" generates code for indexing into the head of a set of clauses
  26.       for a predicate.                            */
  27.  
  28. /* **********************************************************************
  29. $index1_export([$index/8]).
  30.  
  31. $index1_use($glob,[_,$gennum/1,_]).
  32. $index1_use($blist,[_,$member/2,$member1/2]).
  33. $index1_use($meta,[_,_,$length/2]).
  34. $index1_use($listutil1,[_,_,_,_,$nthmember/3,_,_,_]).
  35. $index1_use($bmeta,[_,_,_,$number/1,_,_,_,_,_]).
  36. ********************************************************************** */
  37.  
  38. /* argument 8 of $index is 1 if a switchonlist instruction was generated
  39.    (in which case special code must be generated for the clauses), 0 o/w */
  40.  
  41. $index(P,N,Clauses,PPrag,Pil,Pilr,Prag,SwList) :-
  42.      $worth_indexing(N,Clauses,Prag,Narg) ->
  43.           $index0(P,N,Clauses,Narg,Pil,Pilr,Prag,SwList) ;
  44.       (SwList = 0,
  45.        $index_noarg_chain(Clauses,Chain),
  46.        $gennum(K),
  47.        $index_gen0(N,Chain,(Pred,N,K),Pil,Pilr)
  48.       ).
  49.  
  50. $index0(P,A,Clauses,Narg,[label(PLabel)|Pil],Pilr,Prag,SwList) :-
  51.         $gennum(N),  PLabel = (P,A,N),
  52.     $index1(Clauses,Narg,ArgList),
  53.     $mkindex(ArgList,Narg,PLabel,Pil,Pil2,T,SwList),
  54.     ((T =:= 1,                       /* index created */
  55.       symtype('_$mode'(_,_,_),ModeDef), ModeDef > 0,
  56.       '_$mode'(P,A,Mode),
  57.       $nthmember(BoundArg,Mode,Narg),
  58.       BoundArg > 0                  /* indexed arg is bound */
  59.      ) ->
  60.           Pil2 = Pilr ;
  61.           ($index_noarg_chain(Clauses, Chain),
  62.            $index_gen0(A, Chain, _, Pil2, Pilr)
  63.           )
  64.     ).
  65.  
  66. $index_gen_tab([], _, Pil, Pil).
  67. $index_gen_tab([argt(T, Arg, (_, Arity, N))|Rest], P, 
  68.         [arglabel(T, Arg, (P, Arity, N))|Pil], Pilr) :-
  69.     $index_gen_tab(Rest, P, Pil, Pilr).
  70.  
  71.  
  72. $index1([],_,[]).
  73. $index1([clause(Arglist,_,P)|CRest], N, [argt(Type,NewArg,L)|AtypeRest]) :-
  74.         $nthmember(Arg, Arglist, N),
  75.     $member1(label(L),P),
  76.     $index_getargtype(Arg,argt(Type, NewArg, L)),
  77.     $index1(CRest,N,AtypeRest).
  78.  
  79. $index_getargtype([[]], argt(n, [], _)).
  80. $index_getargtype([X], argt(i, X, _)) :- $number(X).
  81. $index_getargtype([C], argt(c,C,_)).
  82. $index_getargtype(v(_,_),argt(v,v,_)).
  83. $index_getargtype([.|A], argt(l,l,_)).
  84. $index_getargtype([S|A],argt(s,(S, Ar),_)) :- $length(A, Ar), Ar =\= 0.
  85.  
  86.  
  87. $index_noarg_chain([],[]).
  88. $index_noarg_chain([clause(_,_,Prag)|ClRest],[lab(Label)|LRest]) :-
  89.     $member1(label(Label),Prag),
  90.     $index_noarg_chain(ClRest,LRest).
  91.  
  92. :- index($index_gen0, 5, 2).
  93.  
  94. $index_gen0(_,[],abs(-1),Pil,Pil).
  95. $index_gen0(_,[lab(Label)],Label,Pil,Pil) :-
  96.     (var(Label), Label = (sot_label, -1, N), $gennum(N)) ; nonvar(Label).
  97. $index_gen0(Arity,[lab(Label)|Rest],L,[label(L),try(Label,Arity)|Pil],Pilr) :-
  98.     Rest = [_|_],
  99.     ((var(L), L = (sot_label, -1, N), $gennum(N)) ; nonvar(L)),
  100.     $index_gen1(Arity,Rest,Pil,Pilr).
  101.  
  102. $index_gen1(Arity,[lab(L1)],[trust(L1,Arity)|Pilr],Pilr).
  103.  
  104. $index_gen1(Arity,[lab(L1)|Rest],[retry(L1,Arity)|Pil],Pilr) :-
  105.     Rest = [_|_],
  106.     $index_gen1(Arity,Rest,Pil,Pilr).
  107.  
  108. $index_sot_inst(i,l,N,L1,L2,switchonterm(N,L1,L2),0).
  109. $index_sot_inst(i,s,N,L1,L2,switchonterm(N,L1,L2),0).
  110. $index_sot_inst(n,l,N,L1,L2,Inst,SwList) :-
  111.      N =:= 1 ->
  112.           (L1 = (P,K,L1a), L2 = (_,_,L2a),
  113.        Inst = switchonlist(N,(P,K,L1nil),(P,K,L2lis)),
  114.        SwList = 1,
  115.            $concat_atom(L1a,nil,L1nil),
  116.            $concat_atom(L2a,lis,L2lis)
  117.       ) ;
  118.       (Inst = switchonterm(N,L1,L2), SwList = 0).
  119. $index_sot_inst(n,s,N,L1,L2,switchonterm(N,L1,L2),0).
  120. $index_sot_inst(c,l,N,L1,L2,switchonterm(N,L1,L2),0).
  121. $index_sot_inst(c,s,N,L1,L2,switchonterm(N,L1,L2),0).
  122. $index_sot_inst(l,i,N,L1,L2,switchonterm(N,L2,L1),0).
  123. $index_sot_inst(s,i,N,L1,L2,switchonterm(N,L2,L1),0).
  124. $index_sot_inst(l,n,N,L1,L2,Inst,SwList) :-
  125.      N =:= 1 ->
  126.           (L1 = (P,K,L1a), L2 = (_,_,L2a),
  127.        Inst = switchonlist(N,(P,K,L2nil),(P,K,L1lis)),
  128.        SwList = 1,
  129.            $concat_atom(L2a,nil,L2nil),
  130.            $concat_atom(L1a,lis,L1lis)
  131.       ) ;
  132.       (Inst = switchonterm(N,L2,L1), SwList = 0).
  133. $index_sot_inst(s,n,N,L1,L2,switchonterm(N,L2,L1),0).
  134. $index_sot_inst(l,c,N,L1,L2,switchonterm(N,L2,L1),0).
  135. $index_sot_inst(s,c,N,L1,L2,switchonterm(N,L2,L1),0).
  136.  
  137. % $worth_indexing/4 succeeds if, given the arity, clauses and indexing
  138. % pragma for a predicate, it deems it worthwhile to create an index on
  139. % that predicate.  If it does, then the fourth argument is bound to the
  140. % argument number to index on.
  141.  
  142. $worth_indexing(N,Cl,Prag,Narg) :-
  143.      N > 0,
  144.      Cl ?= [_,_|_],    % at least 2 clauses
  145.      $member1(index(Narg), Prag),
  146.      Narg > 0.
  147.  
  148. $mkindex(ArgList,Narg,_,[Inst|Pil2],Pil2,1,SwList) :-
  149.      ArgList = [argt(T1,_,L1),argt(T2,_,L2)],
  150.      $index_sot_inst(T1,T2,Narg,L1,L2,Inst,SwList), !.
  151. $mkindex(ArgList,Narg,PLabel,Pil,Pil2,T,0) :-
  152.      $index_yn(ArgList,1,T),
  153.      (T =:= 1 ->
  154.           (PLabel = (P,A,N),
  155.        $length(ArgList, NCl),
  156.            Pil = [pred(PLabel,NCl)|Pil1],
  157.        $index_gen_tab(ArgList, P, Pil1, Pil3),
  158.        Pil3 = [switchonbound(Narg, N, N)|Pil2]
  159.       ) ;
  160.       Pil2 = Pil
  161.      ).
  162.  
  163. $index_yn([],T,T).
  164. $index_yn([argt(X,_,_)|Rest],T1,T) :-
  165.      X ?= v ->
  166.           T = 0 ;
  167.       $index_yn(Rest,T1,T).
  168.  
  169. /* ------------------------------ $index1.P ------------------------------ */
  170.  
  171.